//
//  SmallDecisionsDpColorsView.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/25.
//

#import "SmallDecisionsDpColorsView.h"
@interface SmallDecisionsDpColorsViewCell: UICollectionViewCell

@property (nonatomic, strong) UIView *dpContainerView;
@property (nonatomic, assign) BOOL isSelected;

@end

@implementation SmallDecisionsDpColorsViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        [self dpContainerView];
    }
    return self;
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    self.dpContainerView.layer.borderWidth = 2;
    self.dpContainerView.layer.masksToBounds = YES;
    self.dpContainerView.layer.borderColor = isSelected ? SmallDecisionsThemeColor.CGColor : [UIColor clearColor].CGColor;

}

- (UIView *)dpContainerView {
    if(!_dpContainerView) {
        _dpContainerView = [[UIView alloc] init];
        _dpContainerView.backgroundColor     = [UIColor clearColor];
        _dpContainerView.layer.cornerRadius  = 12;
        [self.contentView addSubview:_dpContainerView];
        [_dpContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(0);
            make.right.bottom.mas_equalTo(0);
        }];
    }
    return _dpContainerView;
}

@end

@interface SmallDecisionsDpColorsView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,assign) NSUInteger selectedIndex;

@end

@implementation SmallDecisionsDpColorsView
- (instancetype)init  {
    self = [super init];
    if(self) {
        self.selectedIndex = 0;
        self.colorList = [NSArray arrayWithObjects:[UIColor redColor],[UIColor cyanColor],[UIColor orangeColor],[UIColor blueColor],[UIColor yellowColor],[UIColor greenColor],[UIColor purpleColor],[UIColor whiteColor],[UIColor grayColor],[UIColor systemPinkColor], nil];
        [self collectionView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.colorList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsDpColorsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDecisionsDpColorsViewCellId" forIndexPath:indexPath];
    UIColor *bgColor = self.colorList[indexPath.item];
    cell.dpContainerView.backgroundColor = bgColor;
    cell.isSelected = self.selectedIndex == indexPath.item;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    UIColor *color = self.colorList[indexPath.item];
    self.selectedIndex = indexPath.item;
    [collectionView reloadData];
    if(self.selectFinishBlock) {
        self.selectFinishBlock(color);
    }
}


- (UICollectionView *)collectionView {
    if(!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemW = 46;
        CGFloat itemH = 30;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.itemSize = CGSizeMake(itemW, itemH);
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.minimumLineSpacing = 10; // cell 间距

        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[SmallDecisionsDpColorsViewCell class] forCellWithReuseIdentifier:@"SmallDecisionsDpColorsViewCellId"];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.bottom.trailing.mas_equalTo(0);
        }];
    }
    return _collectionView;
}
@end
